package com.life.controller;

import java.awt.Dimension;
import java.util.ArrayList;

import com.life.constants.Constants;
import com.life.model.LifeModel;
import com.life.view.AbstractAdapter;

public class GenerationController extends AbstractController {

	AbstractAdapter applyButton = null;

	AbstractAdapter spinnerNumber = null;

	AbstractAdapter clearButton = null;

	public GenerationController(LifeModel model,
			ArrayList<AbstractAdapter> adapterList) {
		super(model, adapterList);

		applyButton = adapterList.get(0);
		spinnerNumber = adapterList.get(1);
		clearButton = adapterList.get(2);

	if (Constants.CREATE_LIFE_ON_STARTUP)
	{
	    createLife();
	}
	}

	public void createLife()
	{
		theModel.reset();
		int value =Integer.parseInt(spinnerNumber.get().toString());
		
		Dimension dim = theModel.getSize();
		
		for(int i = 0; i < value; i++)
		{
			int x = (int)(dim.getWidth() * Math.random());
			int y = (int)(dim.getHeight() * Math.random());
			
			theModel.setAlive(x,y);
		}
		theModel.fireOnUpdateEvent();
	}
	
	@Override
	public void doUpdate(AbstractAdapter source, Object data) {

		if (source == applyButton) {
			createLife();
		}
		if (source == clearButton) {
			theModel.reset();
			theModel.fireOnUpdateEvent();
		}
	}

}
